package com.etrans.web.gps.business.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.system.domain.ModuleVO;
import com.etrans.web.gps.business.system.service.ModuleService;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.common.page.PageResultVo;
import com.etrans.web.gps.common.page.PageVo;

/**
 * 功能菜单模块管理
 * @author jw.chen
 *
 */
@Controller
@RequestMapping("/system/module/*")
class ModuleController extends BaseController{

	@Autowired
	private ModuleService service;
	
	@RequestMapping(value = "list.html")
	public ModelAndView list(HttpServletRequest request, Model model) {
		ModelAndView mnv = new ModelAndView();
		mnv.addObject("beanForm", new ModuleVO());
		mnv.setViewName("views/business/system/module/queryModule");
		return mnv;
	}
	
	/**
	 * 加载菜单
	 * @param request
	 * @param response
	 * @param isHide		是否隐藏“停用”状态
	 */
	@RequestMapping(value = "loadAll.html")
	public void loadAll(HttpServletRequest request, HttpServletResponse response,String isHide) {
		PageResultVo pr = new PageResultVo();
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("isHide", isHide);
			List rows = service.queryAll(param);
			pr.setData(rows);
			pr.setCode(Constant.RETURN_OK_CODE);
			pr.setMsg("OK");
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("ERROR");
		}

		ajaxResultJson(response, pr.getData());
	}
	
	
	@RequestMapping(value = "loadAllByUser.html")
	public void loadAllByUser(HttpServletRequest request, HttpServletResponse response) {
		PageResultVo pr = new PageResultVo();
		try {
//			Map<String, Object> param = getAuthParam(request);
			Map<String, Object> param = new HashMap<String, Object>();
			List rows = service.queryAllUser(param);
			pr.setData(rows);
			pr.setCode(Constant.RETURN_OK_CODE);
			pr.setMsg("OK");
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("ERROR");
		}

		ajaxResultJson(response, pr.getData());
	}
	
	@RequestMapping(value = "saveModule.html")
	public void saveModule(HttpServletResponse response, ModuleVO form) {
		PageResultVo pr = new PageResultVo();
		try {			
			service.save(form);
			
			PageVo pageBean = new PageVo();
			pageBean.setPage(form.getFunctionId());
			pr.setData(pageBean);
			pr.setCode(Constant.RETURN_OK_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("保存失败!");
		}
		ajaxResultJson(response, pr);
	}
	
	@RequestMapping(value = "updateModule.html")
	public void updateModule(HttpServletResponse response, ModuleVO form) {
		PageResultVo pr = new PageResultVo();
		try {			
			service.update(form);
			
			PageVo pageBean = new PageVo();
			pageBean.setPage(form.getFunctionId());
			pr.setData(pageBean);
			pr.setCode(Constant.RETURN_OK_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("保存失败!");
		}
		ajaxResultJson(response, pr);
	}
	
	@RequestMapping(value = "init.html")
	public ModelAndView init(@RequestParam String id) {
		ModelAndView mav = new ModelAndView();
		ModuleVO modueVo = new ModuleVO();
		modueVo.setParentFuncId(Integer.parseInt(id));
		
		mav.addObject("beanForm", modueVo);
		mav.setViewName("views/business/system/module/addModuleForm");
		return mav;
	}
	
	@RequestMapping("queryById.html")
	public ModelAndView queryById(@RequestParam String id) {
		ModelAndView mav = new ModelAndView();
		
		if(id!=null && !"".equals(id)){
			mav.addObject("beanForm", service.queryById(id));
		}else{
			mav.addObject("beanForm", new ModuleVO());
		}
		mav.setViewName("views/business/system/module/addModuleForm");
		return mav;
	}
	
	@RequestMapping(value="delete.html")
	public void batchDelete(HttpServletResponse response,String id){
		PageResultVo pr = new PageResultVo();
		try {
			if(id != null && !"".equals(id)){
				service.delete(id);
			}
			pr.setCode(Constant.RETURN_OK_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("删除失败!");
		}
		
		ajaxResultJson(response, pr);
	}
	
}
